package com.ruoyi.combat.service.externalServiceInvoker;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.domain.CombatMethod;
import com.ruoyi.combat.domain.PredictRelationshipInfo;
import com.ruoyi.combat.mapper.CombatMethodMapper;
import com.ruoyi.combat.util.MethodUseUtils;
import com.ruoyi.common.exception.base.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Service
public class AntiIpHomingService {

    private static final Logger log = LoggerFactory.getLogger(AntiIpHomingService.class);
    @Autowired
    private CombatMethodMapper combatMethodMapper;
    @Autowired
    private MethodUseUtils methodUseUtils;

    /**
     * 确定IP-组织归属关系抗归属
     */
    public void antiIpHoming(String ip, String oldOrg, String newOrg, String fanType) {
        log.info("确定IP-组织归属关系抗归属");
        try {
            Map<String, Object> antiIpHomingStatus = getAntiIpHomingStatus();
            String status =  (String)antiIpHomingStatus.get("status");
            if ("success".equals(status)) {
                // 先停止
                Map<String, Object> stopAntiIpHoming = stopAntiIpHoming();
            }

            // 更新代理
            CombatMethod combatMethod = combatMethodMapper.selectCombatMethodById(35L);
            if (combatMethod == null) {
                log.error("未查询到更新代理的算法");
                throw new BaseException("未查询到更新代理的算法");
            }
            String methodUrl = combatMethod.getMethodUrl();
            if (StrUtil.isBlank(methodUrl)) {
                log.error("未查询到更新代理的请求地址");
                throw new BaseException("未查询到更新代理的请求地址");
            }
            // http post请求，给请求头添加参数
            String proxypass = "http://" + ip;
            Map<String, Object> proxypassMap = new HashMap<>();
            proxypassMap.put("proxypass", proxypass);
            String proxypassResult = methodUseUtils.methodUseLogs(35L, proxypassMap, "admin", "password");
            // 获取响应体
            Map<String, Object> body = JSONUtil.parseObj(proxypassResult);
            String proxypassStatus = (String)body.get("status");
            if ("success".equals(proxypassStatus)) {
                // 替换组织
                CombatMethod combatMethod2 = combatMethodMapper.selectCombatMethodById(36L);
                if (combatMethod2 == null) {
                    log.error("未查询到替换组织的算法");
                    throw new BaseException("未查询到替换组织的算法");
                }
                String methodUrl2 = combatMethod2.getMethodUrl();
                if (StrUtil.isBlank(methodUrl2)) {
                    log.error("未查询到替换组织的请求地址");
                    throw new BaseException("未查询到替换组织的请求地址");
                }
                List<Map<String, Object>> orgList = new ArrayList<>();
                Map<String, Object> orgMap = new HashMap<>();
                orgMap.put("old", oldOrg);
                orgMap.put("new", newOrg);
                orgMap.put("ip", ip);
                orgMap.put("fanType", fanType);
                orgList.add(orgMap);
                String result = methodUseUtils.methodUseLogs(36L, orgList, "admin", "password");
                Map<String, Object> body2 = JSONUtil.parseObj(result);
                String replaceStatus = (String) body2.get("status");

                if ("success".equals(replaceStatus)) {
                    //开启抗归属
                    CombatMethod combatMethod3 = combatMethodMapper.selectCombatMethodById(37L);
                    if (combatMethod3 == null) {
                        log.error("未查询到开启抗归属的算法");
                        throw new BaseException("未查询到开启抗归属的算法");
                    }
                    String methodUrl3 = combatMethod3.getMethodUrl();
                    if (StrUtil.isBlank(methodUrl3)) {
                        log.error("未查询到开启抗归属的请求地址");
                        throw new BaseException("未查询到开启抗归属的请求地址");
                    }
                    Map<String, Object> map = null;
                    String startResult = methodUseUtils.methodUseLogs(37L, map, "admin", "password");
                    Map<String, Object> body3 = JSONUtil.parseObj(startResult);
                    if (!"success".equals(body3.get("status"))) {
                       throw new BaseException("开启抗归属失败");
                    }
                } else {
                    throw new BaseException("开启抗归属失败");
                }
            } else {
                throw new BaseException("开启抗归属失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BaseException("确定IP-组织归属关系抗归属请求失败");
        }
    }

    /**
     * 预测IP-组织归属关系抗归属
     */
    public Map<String, Object> predictIpHoming(List<String> ips) {
        log.info("预测IP-组织归属关系抗归属");
        try {
            // 获取外部请求接口信息
            CombatMethod combatMethod = combatMethodMapper.selectCombatMethodById(27L);
            if (combatMethod == null) {
                log.error("未查询到预测IP-组织归属关系抗归属");
                throw new BaseException("未查询到预测IP-组织归属关系抗归属");
            }
            String methodUrl = combatMethod.getMethodUrl();
            if (StrUtil.isBlank(methodUrl)) {
                log.error("未查询到预测IP-组织归属关系抗归属算法的请求地址");
                throw new BaseException("未查询到预测IP-组织归属关系抗归属算法的请求地址");
            }
            PredictRelationshipInfo predictRelationshipInfo = new PredictRelationshipInfo();
            predictRelationshipInfo.setIps(ips);
            String param = JSONUtil.toJsonStr(predictRelationshipInfo);
            Map<String, Object> stringObjectMap = methodUseUtils.methodUseLogs(27L, param);
            if (stringObjectMap == null) {
                log.error("预测IP-组织归属关系抗归属请求失败");
                throw new BaseException("预测IP-组织归属关系抗归属请求失败");
            }
            return stringObjectMap;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BaseException("预测IP-组织归属关系抗归属请求失败");
        }
    }

    /**

    /**
     * 确定风险抗归属停止
     */
    public Map<String, Object> stopAntiIpHoming() {
        log.info("确定风险抗归属停止请求");
        try {
            // 获取外部请求接口信息
            CombatMethod combatMethod = combatMethodMapper.selectCombatMethodById(38L);
            if (combatMethod == null) {
                log.error("未查询到确定风险抗归属的停止算法");
                throw new BaseException("未查询到确定风险抗归属的停止算法");
            }
            String methodUrl = combatMethod.getMethodUrl();
            if (StrUtil.isBlank(methodUrl)) {
                log.error("未查询到确定风险抗归属的停止算法的请求地址");
                throw new BaseException("未查询到确定风险抗归属的停止算法的请求地址");
            }
            Map<String, Object> map = null;
            String result = methodUseUtils.methodUseLogs(38L, map, "admin", "password");
            // 获取响应体
            Map<String, Object> body = JSONUtil.parseObj(result);
            return body;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BaseException("确定风险抗归属停止请求失败");
        }
    }
    public Map<String, Object> getAntiIpHomingStatus() {
        try {
            log.info("查看确定风险抗归属程序状态");
            // 获取外部请求接口信息
            CombatMethod combatMethod = combatMethodMapper.selectCombatMethodById(34L);
            if (combatMethod == null) {
                log.error("未查询到确定风险抗归属程序状态的算法");
                throw new BaseException("未查询到确定风险抗归属程序状态的算法");
            }
            String methodUrl = combatMethod.getMethodUrl();
            if (StrUtil.isBlank(methodUrl)) {
                log.error("未查询到确定风险抗归属程序状态的算法的请求地址");
                throw new BaseException("未查询到确定风险抗归属程序状态的算法的请求地址");
            }
            Map<String, Object> map = null;
            String result = methodUseUtils.methodUseLogs(34L, map, "admin", "password");
            // 获取响应体
            Map<String, Object> body = JSONUtil.parseObj(result);
            return body;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BaseException("查看抗归属程序状态请求失败");
        }
    }

    /**
     * 关闭抗归属程序
     */
    public void closeAntiIpHoming() {
        log.info("关闭抗归属程序");
    }

    /**
     * 预测IP-组织归属关系
     */
    public Map<String, Object> predictRelationship(List<String> ips) {
        try {
            log.info("查询预测组织和IP关系");
            // 获取外部请求接口信息
            CombatMethod combatMethod = combatMethodMapper.selectCombatMethodById(26L);
            if (combatMethod == null) {
                log.error("未查询到预测组织和IP关系的算法");
                throw new BaseException("未查询到预测组织和IP关系的算法");
            }
            String methodUrl = combatMethod.getMethodUrl();
            if (StrUtil.isBlank(methodUrl)) {
                log.error("未查询到预测组织和IP关系的算法的请求地址");
                throw new BaseException("未查询到预测组织和IP关系的算法的请求地址");
            }
            PredictRelationshipInfo predictRelationshipInfo = new PredictRelationshipInfo();
            predictRelationshipInfo.setIps(ips);
            String param = JSONUtil.toJsonStr(predictRelationshipInfo);
            Map<String, Object> stringObjectMap = methodUseUtils.methodUseLogs(26L, param);
            Map<String, Object> results = (Map<String, Object>) stringObjectMap.get("results");
            if (results == null) {
                log.error("预测组织和IP关系的算法请求失败");
                throw new BaseException("预测组织和IP关系的算法请求失败");
            }
            return results;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BaseException("预测组织和IP关系的算法请求失败");
        }
    }

    /**
     * 预测IP-组织关系查看抗归属状态
     */
    public Map<String, Object> getPredictRelationshipStatus(List<String> ips) {
        try {
            log.info("预测IP-组织关系，查看抗归属状态");
            // 获取外部请求接口信息
            CombatMethod combatMethod = combatMethodMapper.selectCombatMethodById(33L);
            if (combatMethod == null) {
                log.error("未查询到预测IP-组织关系，查看抗归属状态的算法");
                throw new BaseException("未查询到预测IP-组织关系，查看抗归属状态的算法");
            }
            String methodUrl = combatMethod.getMethodUrl();
            if (StrUtil.isBlank(methodUrl)) {
                log.error("未查询到预测IP-组织关系，查看抗归属状态算法的请求地址");
                throw new BaseException("未查询到预测IP-组织关系，查看抗归属状态算法的请求地址");
            }
            PredictRelationshipInfo predictRelationshipInfo = new PredictRelationshipInfo();
            predictRelationshipInfo.setIps(ips);
            String param = JSONUtil.toJsonStr(predictRelationshipInfo);
            Map<String, Object> stringObjectMap = methodUseUtils.methodUseLogs(33L, param);
            if (stringObjectMap != null) {
                if (stringObjectMap.get("adversarial_status") != null) {
                    String status = (String)stringObjectMap.get("adversarial_status");
                    if ("对抗停止".equals(status)) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("adversarial_status", "对抗停止");
                        return map;
                    }
                }
            }
            return null;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BaseException("预测IP-组织关系，查看抗归属状态的算法请求失败");
        }
    }

}
